#!/bin/bash
pathSvn="/home/niki/eXist/webapp/xquery/project-db-uh"
pathExist="/home/niki/eXist"

usernameExist="admin"
passwordExist="projectdb"

#Compile if necessary
if [ ! -e $pathSvn/carrot/bin/Clustering.class ] || [ ! -e $pathSvn/carrot/bin/SpeakerData.class ] || test $pathSvn/carrot/src/#SpeakerData.java -nt $pathSvn/carrot/bin/SpeakerData.class || test $pathSvn/carrot/src/Clustering.java -nt $pathSvn/carrot/bin/Clustering.class
then
	echo "\n* Compiling java prog for clustering..."
	outputJava=`javac -classpath "$pathSvn/carrot/lib/required/*:$pathSvn/carrot/lib/optional/*:." -d $pathSvn/carrot/bin/ $pathSvn/carrot/src/Clustering.java $pathSvn/carrot/src/SpeakerData.java`
	echo "\n* Compilation completed!"
else
	echo "\n* No need to compile."
fi

echo "\n* Executing preparation query for clustering"
outputPrepQuery=`$pathExist/bin/client.sh -u $usernameExist -P $passwordExist -F $pathSvn/update_queries/calculateInputDataForClustering.xquery`

echo "\n* Starting clustering..."
outputClustering=`java -classpath "$pathSvn/carrot/lib/required/*:$pathSvn/carrot/lib/optional/*:$pathSvn/carrot/bin/." Clustering`

echo "\n* storing cluster-results in database"
ouputStore=`$pathExist/bin/client.sh -u $usernameExist -P $passwordExist -m /db/projdb/ -p $pathSvn/clusterdata.xml`

outputRm=`rm $pathSvn/clusterdata.xml`

echo "\n* Executing query 1 of 9"
outputQuery1=`$pathExist/bin/client.sh -u $usernameExist -P $passwordExist -F $pathSvn/update_queries/onderbrekingsdataquery.xquery`
echo `expr "$outputQuery1" : '.*\(<message>.*</message>\)'`

echo "\n* Executing query 2 of 9"
outputQuery2=`$pathExist/bin/client.sh -u $usernameExist -P $passwordExist -F $pathSvn/update_queries/onderbrekingsdataquery_year.xquery`
echo `expr "$outputQuery1" : '.*\(<message>.*</message>\)'`

echo "\n* Executing query 3 of 9"
outputQuery3=`$pathExist/bin/client.sh -u $usernameExist -P $passwordExist -F $pathSvn/update_queries/update_list_of_initiators_and_participants.xquery`
echo `expr "$outputQuery2" : '.*\(<message>.*</message>\)'`

echo "\n* Executing query 4 of 9"
outputQuery4=`$pathExist/bin/client.sh -u $usernameExist -P $passwordExist -F $pathSvn/update_queries/update_participatie_parameters.xquery`
echo `expr "$outputQuery3" : '.*\(<message>.*</message>\)'`

echo "\n* Executing query 5 of 9"
outputQuery5=`$pathExist/bin/client.sh -u $usernameExist -P $passwordExist -F $pathSvn/update_queries/update_participatie_parameters_year.xquery`
echo `expr "$outputQuery3" : '.*\(<message>.*</message>\)'`

echo "\n* Executing query 6 of 9"
outputQuery6=`$pathExist/bin/client.sh -u $usernameExist -P $passwordExist -F $pathSvn/update_queries/calculateClusterData.xquery`
echo `expr "$outputQuery4" : '.*\(<message>.*</message>\)'`

echo "\n* Executing query 7 of 9"
outputQuery7=`$pathExist/bin/client.sh -u $usernameExist -P $passwordExist -F $pathSvn/update_queries/sortClusterDataBySpeakers.xquery`
echo `expr "$outputQuery5" : '.*\(<message>.*</message>\)'`

echo "\n* Executing query 8 of 9"
outputQuery8=`$pathExist/bin/client.sh -u $usernameExist -P $passwordExist -F $pathSvn/update_queries/calculateMostPopularClusters.xquery`
echo `expr "$outputQuery6" : '.*\(<message>.*</message>\)'`

echo "\n* Executing query 9 of 9"
outputQuery9=`$pathExist/bin/client.sh -u $usernameExist -P $passwordExist -F $pathSvn/update_queries/update_topic_titles_for_subjects.xquery`
echo `expr "$outputQuery7" : '.*\(<message>.*</message>\)'`

echo "Done!"
